package ji.sky.accountdemo;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;

import java.util.ArrayList;

public class MainChartFragment extends Fragment {
    private PieChart chartAccount;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.main_chart,container,false);

        chartAccount = view.findViewById(R.id.chart_account);
        chartAccount.setDrawHoleEnabled(false);
        Description description = new Description();
        description.setText("消费分类统计图");
        chartAccount.setDescription(description);

        ArrayList<PieEntry> entries = new ArrayList<>();
        entries.add(new PieEntry(40.f, "用餐"));
        entries.add(new PieEntry(27.f, "服装"));
        entries.add(new PieEntry(10.f, "交通"));
        entries.add(new PieEntry(23.f, "娱乐"));
        PieDataSet dataSet = new PieDataSet(entries,"");
        dataSet.setValueTextSize(18.f);
        ArrayList<Integer> colors = new ArrayList<>();
        colors.add(Color.rgb(205,205,205));
        colors.add(Color.rgb(114,188,223));
        colors.add(Color.rgb(205,123,205));
        colors.add(Color.rgb(57,135,205));
        dataSet.setColors(colors);
        PieData pieData = new PieData(dataSet);
        pieData.setValueFormatter(new PercentFormatter());
        chartAccount.setData(pieData);
        chartAccount.invalidate();

        return view;
    }
}
